package com.kaifamiao.month04.day11;

import java.util.Scanner;

public class Demo {

    public static int maxLength(String str) {
        if (str == null) {
            return -1;
        }
        int max = 1;
        for (int start = 0,end = 0; end < str.length(); end ++) {
            String child = str.substring(start , end);
            if (child.contains(str.charAt(end) + "")) {
                max = Math.max(child.length(),max);
            }else if (end == str.length() - 1) {
                max = Math.max(child.length() + 1, max);
            }
        }
        return max;
    }

    public static int maxLength2(String str) {
        int max = 1;
        StringBuilder child = new StringBuilder();
        for (int i = 0; i < str.length(); i ++) {
           String current = Character.toString(str.charAt(i));
           int index = child.indexOf(current);
           if (child.toString().contains(current)) {
                max = Math.max(child.length(), max);
                if (max > str.length() - index - 1) {
                    return max;
                }
                child = new StringBuilder(child.substring(index + 1));
           }
           child.append(current);
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println("请输入一个字符串：");
        Scanner in = new Scanner(System.in);
        String str = in.next();
        System.out.println(maxLength2(str));
    }
}
